apiready = function(){
seajs.use(['../../V',"../../api","../../jquery","../../app","../../service","../../swiper"],function(V,$api,$,app,service,Swiper){
	var vm = {
		el: '.container',
	  	data: {
        shopInfo: {},
        type: '',
        classifyList: [],
        goodsList: [],
				page: 1,
				classfiyid: '',
				hasMoreData: true,
				userInfo: $api.getStorage("user").data, // 获取用户身份
				isSearch: false,
				searchVal: ''
	  	},
			created: function(){
				var that = this;
        var shopInfo = api.pageParam.shopInfo;
        var type = api.pageParam.type;

        this.type = type;
        if (type == 'all') {
          $('.title').text('全部商品');
        } else if (type == 'hot') {
          $('.title').text('热门商品');
        } else if (type == 'yzc') {
					$('.title').text('YZC商品');
				}

				api.addEventListener({
				    name: 'search-goods'
				}, function(ret, err){
				    if(ret){
				      var val = $('.search-goods').val();
							that.searchVal = val;
							that.page = 1;
							that.searchGoods();
				    }
				});


        this.shopInfo = shopInfo;

        this.init();

				// 触底
				api.addEventListener({
			 	    name: 'scrolltobottom',
						extra:{
				        threshold:0  //设置距离底部多少距离时触发，默认值为0，数字类型
				    }
			 	}, function(ret, err){
						that.scrolltobottom()
			 	});
	  	},
	  	methods:{
        init: function () {
          this.getClassfiy();
          this.getGoodsList();
        },
        getClassfiy: function () {
          var that = this;
					app.ajax(
						service['getclassifyList'],
						{
							data: {
								values: {
									unionid: that.shopInfo.unionid,
									shop_code: that.shopInfo.shop_code,
								}
							}
						},
						function (res) {
							if (res.code == 0) {
								that.classifyList = res.data;
							} else {
								api.toast({
										msg: res.message,
										duration: 2000,
										location: 'bottom'
								});
							}
						}
					);
        },

        // 获取全部商品
        getGoodsList: function () {
          var that = this;
          var def_arg;
					var url = service['getGoodsList'];

          if (that.type == 'all') {
            // 获取全部商品
            if (that.classfiyid != -1) {
              def_arg = {
                unionid: that.shopInfo.unionid,
                shop_code: that.shopInfo.shop_code,
                category_id: that.classfiyid,
								page: that.page
              }
            } else {
              def_arg = {
                unionid: that.shopInfo.unionid,
                shop_code: that.shopInfo.shop_code,
								page: that.page
              }
            }
          } else if (that.type == 'hot') {
            // 获取热门商品
            if (that.classfiyid != -1) {
              def_arg = {
                unionid: that.shopInfo.unionid,
                shop_code: that.shopInfo.shop_code,
                hot: 1,
                category_id: that.classfiyid,
								page: that.page
              }
            } else {
              def_arg = {
                unionid: that.shopInfo.unionid,
                shop_code: that.shopInfo.shop_code,
                hot: 1,
								page: that.page
              }
            }
          } else if (that.type == 'yzc') {
						// yzc商品
						url = service['getYzcShopGoodsList'];
						// 获取热门商品
            if (that.classfiyid != -1) {
              def_arg = {
                unionid: that.shopInfo.unionid,
                shop_code: that.shopInfo.shop_code,
                hot: 1,
                category_id: that.classfiyid,
								page: that.page
              }
            } else {
              def_arg = {
                unionid: that.shopInfo.unionid,
                shop_code: that.shopInfo.shop_code,
                hot: 1,
								page: that.page
              }
            }
					}

          app.ajax(
            url,
            {
              data: {
                values: def_arg
              }
            },
            function (res) {
              if (res.code == 0) {
								if (res.data.length > 0) {
									for (var i = 0, len = res.data.length; i < len; i++) {
										that.goodsList.push(res.data[i]);
									}
									that.hasMoreData = true;
								} else {
									that.hasMoreData = false;
									api.toast({
									    msg: '没有更多数据了',
									    duration: 2000,
									    location: 'bottom'
									});

								}
              } else {
                api.toast({
                    msg: res.message,
                    duration: 2000,
                    location: 'bottom'
                });
              }
            }
          );
        },

        // 切换分类
        changeList: function (idx, id) {
          var classfiyid = id;
					this.page = 1;
					this.isSearch = false;
					this.classfiyid = id;
					this.goodsList = [];
          $('.type-nav').removeClass('active');
          $('.type-nav').eq(idx).addClass('active');

          this.getGoodsList();
        },

				// 商品详情
				toGoodsDetail: function (goods_id, group, is_yzc) {
					var that = this;
					if (is_yzc) {
						app.openWin('buyGoodsYzc', {goodsId: goods_id});
					} else {
						if (group) {
							app.openWin('groupBuyDetail',{goodsId :goods_id});
						} else {
							app.openWin('buyGoods', {goodsId: goods_id});
						}
					}
				},

				// 搜索商品
				searchGoods: function () {
					if (!this.isSearch) {
						this.goodsList = [];
						this.hasMoreData = true;
					}
					var keyword = this.searchVal;
					$('.type-nav').removeClass('active');
          $('.type-nav').eq(0).addClass('active');
					var that = this;
					if (keyword == '' || keyword == null) {
						api.toast({
						    msg: '请输入关键字',
						    duration: 2000,
						    location: 'middle'
						});
						return;
					}

					var def_arg;
					var url = service['getGoodsList'];
          if (that.type == 'all') {
            // 获取全部商品
						def_arg = {
							unionid: that.shopInfo.unionid,
							shop_code: that.shopInfo.shop_code,
							keyword: keyword,
							page: that.page
						}
          } else if (that.type == 'hot') {
            // 获取热门商品
						def_arg = {
							unionid: that.shopInfo.unionid,
							shop_code: that.shopInfo.shop_code,
							hot: 1,
							keyword: keyword,
							page: that.page
						}
          } else if (that.type == 'yzc') {
						// 获取yzc商品
						url = service['getYzcShopGoodsList'];
						def_arg = {
							unionid: that.shopInfo.unionid,
							shop_code: that.shopInfo.shop_code,
							keyword: keyword,
							is_home: 0,
							page: that.page
						}
					}
          app.ajax(
            url,
            {
              data: {
                values: def_arg
              }
            },
            function (res) {
              if (res.code == 0) {
								that.isSearch = true;
                // that.goodsList = res.data;
								if (res.data.length > 0) {
									res.data.forEach(function (item, index) {
										that.goodsList.push(item);
									});
								} else {
									api.toast({
									    msg: '没有更多数据数据了',
									    duration: 2000,
									    location: 'bottom'
									});
								}
              } else {
                api.toast({
                    msg: res.message,
                    duration: 2000,
                    location: 'bottom'
                });
              }
            }
          );
				},

				// 加载更多
				scrolltobottom: function () {
					if (this.isSearch) {
						if (this.hasMoreData) {
							this.page += 1;
							this.searchGoods();
						}
					} else {
						if (this.hasMoreData) {
							this.page += 1;
							this.getGoodsList();
						}
					}

				}

	  	},
	  	mounted:function() {

	  	}
	};
	var even =function () {
		$('.right-btn').on('click', function () {
			$('.title').hide();
			$('.search-form').show();
			$('.search-goods').focus();
		});

		$('.search-goods').blur(function () {
			$('.search-form').hide();
			$('.title').show();
		});

		$('.search-form').on('submit', function () {
			api.sendEvent({
			    name: 'search-goods'
			});
			return false;
		});
	}
	var newVue = new Vue(vm);
	even();
});
}
